require.config({
    baseUrl:"./modules_cart",
    paths:{
        jq:"../libs/jquery",
        sih:"switch",
        getData:"../modules_productList/getData"
    }
})

require(["jq","sih","appear","options","getData","cartRender","allFlag","isLogin"],function(_,sih,appear,options,getData,cartRender,allFlag,isLogin){
    // 检测是否登录
    const userMsg = document.querySelector(".userMsg");
    const last_a = document.querySelector(".last_a a");
    isLogin(userMsg,last_a)
    const close = document.querySelector(".address_close");
    const changed = document.querySelector(".address_area");
    const apr = document.querySelector(".address_text");
    // 点击使其隐藏
    sih(close,changed);
    // 点击使其出现
    appear(apr, changed);
    // 选项,点击后渲染到元素内
    const ops = document.querySelectorAll(".address_cont li a");
    const city = document.querySelector("#city");
    options(ops, city, apr);
    // 渲染购物车
    const item_cont = document.querySelector(".item_cont");
    const productMsg = localStorage.getItem("productMsg") ? JSON.parse(localStorage.getItem("productMsg")) : [];
    // 商品数量
    const tl_ct = document.querySelector(".total_count");
    // 总计价格
    const tl_pc = document.querySelector(".total_price");
    getData(res=>{
        cartRender(res, productMsg, item_cont);
        // 全选绑定
        const allFl = document.querySelector(".allFlag");
        const inp_ck = document.querySelectorAll(".inp_check");
        allFl.checked = productMsg.every(val=>val.flag);
        allFl.addEventListener("input",()=>{
            allFlag(productMsg,allFl.checked,inp_ck)
            // 计算数量
            let sum = 0;
            for(let i=0;i<productMsg.length;i++){
                if(productMsg[i].flag){
                    sum += productMsg[i].num-0
                }
            }
            tl_ct.innerHTML = sum;
            // 计算总价
            const allP = productMsg.reduce((start,val)=>val.flag ? start+= parseInt(val.num * val.price) : start, 0);
            tl_pc.innerHTML = allP;
            console.log("到这了：多选")
        })
    })
    // 打开之前就计算数量
    let sum = 0;
    for(let i=0;i<productMsg.length;i++){
        if(productMsg[i].flag){
            sum += productMsg[i].num-0
        }
    }
    tl_ct.innerHTML = sum;
    // 打开之前计算总价&合计
    const allP = productMsg.reduce((start,val)=>val.flag ? start+= parseInt(val.num * val.price) : start, 0);
    tl_pc.innerHTML = allP;
})